package com.haha.helloworld;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

public class PopupWindowActivity extends AppCompatActivity {
    private Button btn_pop;
    //定义一个PopupWindow
    private PopupWindow m_pop;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_popup_window);
        btn_pop=(Button)findViewById(R.id.btn_pop);
        btn_pop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View view = getLayoutInflater().inflate(R.layout.layout_pop,null);
                View textView = (TextView)view.findViewById(R.id.tv_good);
                textView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(PopupWindowActivity.this, "不错！." , Toast.LENGTH_SHORT).show();
                        m_pop.dismiss();
                    }
                });
                //给m_pop设置一个view以及宽高
                m_pop=new PopupWindow(view,btn_pop.getWidth(), ViewGroup.LayoutParams.WRAP_CONTENT);
                //点击tv_good外边会取消掉tv_good
                m_pop.setOutsideTouchable(true);
                //点击会使其展开或者收起
                m_pop.setFocusable(true);
                //使其在button下边
                m_pop.showAsDropDown(btn_pop);
            }
        });

    }
}